package com.softgroup.breath.easyecgproject.entry;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.softgroup.breath.easyecgproject.myapi.FormatCast;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static com.softgroup.breath.easyecgproject.utils.IOStreamUtil.readTextFromSDcard;


/**
 * Created by Administrator on 2017/5/17.
 */

public class CommandInfo {
    /**
     * protoversion : 1.51
     * infoquest : [{"questmedia":"both","answerinfo":[{"answerinfoexplain":{},"answerinforule":"hex","answerinfodesc":"软件版本号","answerinfolen":1,"answerinfonum":1},{"answerinfoexplain":{"00":"待机","01":"实时采集","02":"同步采集","03":"单机采集"},"answerinforule":"map","answerinfodesc":"设备状态","answerinfolen":1,"answerinfonum":2},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"电量","answerinfolen":2,"answerinfonum":3}],"answerlen":4,"answertype":"IMMED","questname":"查询状态","questadditon":"","questcode":"E810"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"ascii","answerinfodesc":"设备出厂信息","answerinfolen":11,"answerinfonum":1}],"answerlen":12,"answertype":"IMMED","questname":"查询设备出厂信息","questadditon":"","questcode":"E812"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"ascii","answerinfodesc":"设备型号","answerinfolen":14,"answerinfonum":1}],"answerlen":14,"answertype":"IMMED","questname":"查询设备型号","questadditon":"","questcode":"E813"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":1,"answerinfonum":1},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"数据量","answerinfolen":3,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"查询数据量","questadditon":"","questcode":"E814"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":2,"answerinfonum":1},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"已存数据占比","answerinfolen":2,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"查询已存数据占比","questadditon":"","questcode":"E815"},{"questmedia":"both","answerinfo":[{"answerinfoexplain":{"00":"FLASH正常","01":"FLASH异常"},"answerinforule":"map","answerinfodesc":"FLASH状态","answerinfolen":1,"answerinfonum":1},{"answerinfoexplain":{"00":"ADS正常","01":"ADS异常"},"answerinforule":"map","answerinfodesc":"ADS状态","answerinfolen":1,"answerinfonum":2},{"answerinfoexplain":{"00":"蓝牙正常","01":"蓝牙异常"},"answerinforule":"map","answerinfodesc":"蓝牙状态","answerinfolen":1,"answerinfonum":3},{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":1,"answerinfonum":4}],"answerlen":4,"answertype":"IMMED","questname":"查询错误码","questadditon":"","questcode":"E816"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"存储次数","answerinfolen":4,"answerinfonum":1}],"answerlen":4,"answertype":"IMMED","questname":"查询存储次数","questadditon":"","questcode":"E817"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"毫秒值","answerinfolen":4,"answerinfonum":1}],"answerlen":4,"answertype":"IMMED","questname":"查询毫秒值","questadditon":"","questcode":"E818"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"单个有效页占用字节数PP","answerinfolen":2,"answerinfonum":1},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"FLASH总容量AMB（单位：MB）","answerinfolen":2,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"查询FLASH参数","questadditon":"","questcode":"E819"},{"questmedia":"both","answerinfo":[{"answerinfoexplain":{},"answerinforule":"hex","answerinfodesc":"设备ID","answerinfolen":12,"answerinfonum":1}],"answerlen":12,"answertype":"IMMED","questname":"查询设备ID","questadditon":"","questcode":"E81C"},{"questmedia":"both","answerinfo":[{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"设备时间-年","answerinfolen":1,"answerinfonum":1},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"设备时间-月","answerinfolen":1,"answerinfonum":2},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"设备时间-日","answerinfolen":1,"answerinfonum":3},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"设备时间-时","answerinfolen":1,"answerinfonum":4},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"设备时间-分","answerinfolen":1,"answerinfonum":5},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"设备时间-秒","answerinfolen":1,"answerinfonum":6}],"answerlen":6,"answertype":"IMMED","questname":"查询设备时间","questadditon":"","questcode":"E81F"}]
     * controlquest : [{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"开始同步采集","questadditon":"","questcode":"E821"},{"questmedia":"both","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"开始单机采集","questadditon":"$YMDHFS","questcode":"E823"},{"questmedia":"both","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"结束采集","questadditon":"","questcode":"E822"},{"questmedia":"both","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"装订时间","questadditon":"$YMDHFS","questcode":"E840"},{"questmedia":"both","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"装订用户ID","questadditon":"$USEID18","questcode":"E841"}]
     * version : 0.1
     * erroranswer : [{"errcode":"E8FF","errmsg":"指令有误[长度不足、命令字不存在、逻辑错误]"},{"errcode":"$QUESTCODE","errmsg":"指令执行失败[索要数据不存在、当前时刻不适合执行此命令、设备错误]"}]
     * dataquest : [{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}],"answerlen":4,"answertype":"DELAY","questname":"清除存储数据","questadditon":"","questcode":"E830"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}],"answerlen":4,"answertype":"IMMED","questname":"停止读取存储数据","questadditon":"","questcode":"E832"},{"questmedia":"usb","answerinfo":"","answerlen":0,"answertype":"DELAY","questname":"读取存储数据","questadditon":"$ADDRESS8","questcode":"E831038440000388C000"},{"questmedia":"usb","answerinfo":[{"answerinfoexplain":{},"answerinforule":"hex","answerinfodesc":"设备ID","answerinfolen":12,"answerinfonum":1},{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":1,"answerinfonum":2},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"测试时间-年","answerinfolen":1,"answerinfonum":3},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"测试时间-月","answerinfolen":1,"answerinfonum":4},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"测试时间-日","answerinfolen":1,"answerinfonum":5},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"测试时间-时","answerinfolen":1,"answerinfonum":6},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"测试时间-分","answerinfolen":1,"answerinfonum":7},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"测试时间-秒","answerinfolen":1,"answerinfonum":8},{"answerinfoexplain":{},"answerinforule":"ascii","answerinfodesc":"测试对象ID","answerinfolen":18,"answerinfonum":9},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"放大倍数","answerinfolen":8,"answerinfonum":10},{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":6,"answerinfonum":11},{"answerinfoexplain":{},"answerinforule":"hex","answerinfodesc":"起始地址","answerinfolen":4,"answerinfonum":12},{"answerinfoexplain":{},"answerinforule":"hex","answerinfodesc":"结束地址","answerinfolen":4,"answerinfonum":13},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"页总数","answerinfolen":3,"answerinfonum":14}],"answerlen":64,"answertype":"LOOP","questname":"读取索引数据","questadditon":"","questcode":"E833"}]
     */

    public static final String ANALYSIS_ERROR = "解析错误";
    public final String TAG = getClass().getSimpleName();
    private String protoversion;
    private List<InfoquestEntity> infoquest;
    private List<ControlquestEntity> controlquest;
    private String version;
    private List<ErroranswerEntity> erroranswer;
    private List<DataquestEntity> dataquest;
    private static CommandInfo commandInfo = null;

    private String durationMode = "1770";

    private CommandInfo() {
    }

    public static CommandInfo getInstance(Context context) {
        if (commandInfo == null) {
            synchronized (CommandInfo.class) {
                if (commandInfo == null) {
                    try {
                        InputStream is = context.getAssets().open("command.json");
                        String text = readTextFromSDcard(is);
                        if (commandInfo == null) {
                            Gson gson = new Gson();
                            commandInfo = gson.fromJson(text, CommandInfo.class);
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        Log.d("readFromAssets", e.toString());
                    }
                }
            }
        }
        return commandInfo;
    }

    public static String[] getArrayQuestName() {
        ArrayList<String> commands = new ArrayList<>();
        for (InfoquestEntity ie : commandInfo.getInfoquest()) {
            if (ie.getQuestmedia().equals("blue") | ie.getQuestmedia().equals("both")) {
                commands.add(ie.getQuestname());
            }
        }
        for (DataquestEntity de : commandInfo.getDataquest()) {
            if (de.getQuestmedia().equals("blue") | de.getQuestmedia().equals("both")) {
                commands.add(de.getQuestname());
            }
        }
        for (ControlquestEntity ce : commandInfo.getControlquest()) {
            if (ce.getQuestmedia().equals("blue") | ce.getQuestmedia().equals("both")) {
                commands.add(ce.getQuestname());
            }
        }
        String[] arrayCommands = new String[commands.size()];
        for (int i = 0; i < commands.size(); i++) {
            arrayCommands[i] = commands.get(i);
        }
        return arrayCommands;
    }

    public static String getRealQuest(int subscript) {
        String string = "";

        for (InfoquestEntity ie : commandInfo.getInfoquest()) {
            if (ie.getQuestname().equals(getArrayQuestName()[subscript])) {
                if (ie.getQuestadditon().equals("")) {
                    string = ie.getQuestcode();
                    if (ie.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                } else if (ie.getQuestadditon().equals("$YMDHFS")) {
                    string = ie.getQuestcode() + FormatCast.getDate(true);
                    string += commandInfo.getDurationMode();
                    if (ie.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                } else if (ie.getQuestadditon().equals("$USEID18")) {
                    string = ie.getQuestcode();
                    if (ie.getQuestadditon().equals("IMMED")) {
                        break;
                    }

                } else if (ie.getQuestadditon().equals("$CHANNEL")) {
                    string = ie.getQuestcode() + "01ffffff";
                    if (ie.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                }
            }
        }
        for (DataquestEntity de : commandInfo.getDataquest()) {
            if (de.getQuestname().equals(getArrayQuestName()[subscript])) {
                if (de.getQuestadditon().equals("")) {
                    string = de.getQuestcode();
                    if (de.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                } else if (de.getQuestadditon().equals("$YMDHFS")) {
                    string = de.getQuestcode() + FormatCast.getDate(true);
                    string += commandInfo.getDurationMode();
                    if (de.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                } else if (de.getQuestadditon().equals("$USEID18")) {
                    string = de.getQuestcode();
                    if (de.getQuestadditon().equals("IMMED")) {
                        break;
                    }

                } else if (de.getQuestadditon().equals("$CHANNEL")) {
                    string = de.getQuestcode() + "01ffffff";
                    if (de.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                }
            }
        }
        for (ControlquestEntity ce : commandInfo.getControlquest()) {
            if (ce.getQuestname().equals(getArrayQuestName()[subscript])) {
                if (ce.getQuestadditon().equals("")) {
                    string = ce.getQuestcode();
                    if (ce.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                } else if (ce.getQuestadditon().equals("$YMDHFS")) {
                    string = ce.getQuestcode() + FormatCast.getDate(true);
                    string += commandInfo.getDurationMode();
                    if (ce.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                } else if (ce.getQuestadditon().equals("$USEID18")) {
                    string = ce.getQuestcode();
                    if (ce.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                } else if (ce.getQuestadditon().equals("$CHANNEL")) {
                    string = ce.getQuestcode() + "01ffffff";
                    if (ce.getQuestadditon().equals("IMMED")) {
                        break;
                    }
                }
            }
        }
        return string;
    }

    public static String getRealAnswer(byte[] answer) {
        byte[] bytes = new byte[0];
        if (answer.length > 1) {
            bytes = new byte[]{answer[0], answer[1]};
        }
        String answerString = "";
        int sub = 0;
        for (ErroranswerEntity ee : commandInfo.getErroranswer()) {
            if (ee.getErrcode().equals(FormatCast.bytesToHexString(bytes))) {
                return ee.getErrmsg() + FormatCast.bytesToHexString(answer);
//            } else if (answer.length == 6 && answer[2] == 0x00&& answer[3] == 0x00&& answer[4] == 0x00&& answer[5] == 0x00) {
//                if (ee.getErrcode().equals("$QUESTCODE")) {
//                    return ee.getErrmsg() + bytesToHexString(answer);
//                }
            }
        }
        for (InfoquestEntity ie : commandInfo.getInfoquest()) {
            if ((ie.getQuestmedia().equals("both") | ie.getQuestmedia().equals("blue")) && FormatCast.bytesToHexString(FormatCast.getHexBytes(ie.getQuestcode())).equals(FormatCast.bytesToHexString(bytes))) {
                answerString = ie.getQuestname();
                if (ie.getAnswerlen() == answer.length - 2) {
                    sub += 2;
                    for (InfoquestEntity.AnswerinfoEntity ae : ie.getAnswerinfo()) {
                        if (ae.getAnswerinforule().equals("hex")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += Integer.toHexString(answer[i] & 0xff).length() < 2 ? ("0" + Integer.toHexString(answer[i] & 0xff)) : Integer.toHexString(answer[i] & 0xff);
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        } else if (ae.getAnswerinforule().equals("map")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                switch (answer[sub]) {
                                    case 0:
                                        answerString += ae.getAnswerinfoexplain().getZerozero();
                                        break;
                                    case 1:
                                        answerString += ae.getAnswerinfoexplain().getZeroone();
                                        break;
                                    case 2:
                                        answerString += ae.getAnswerinfoexplain().getZerotwo();
                                        break;
                                    case 3:
                                        answerString += ae.getAnswerinfoexplain().getZerothree();
                                        break;
                                }
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        } else if (ae.getAnswerinforule().equals("dec")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            int a = 0;
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                a = a | ((answer[i] & 0xff) << (8 * (sub + ae.getAnswerinfolen() - i - 1)));
                            }
                            answerString += a;
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        } else if (ae.getAnswerinforule().equals("ascii")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += (char) answer[i];
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        } else if (ae.getAnswerinforule().equals("time")) {
                            answerString += ae.getAnswerinfodesc() + ":" + "20";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += Integer.toString(answer[i] & 0xff).length() < 2 ? ("0" + Integer.toString(answer[i] & 0xff)) : Integer.toString(answer[i] & 0xff);
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        } else if (ae.getAnswerinforule().equals("")) {
                            sub += ae.getAnswerinfolen();
                        }
                    }
                    return answerString;
                }
            }
        }
        for (DataquestEntity de : commandInfo.getDataquest()) {
            if ((de.getQuestmedia().equals("both") | de.getQuestmedia().equals("blue")) && FormatCast.bytesToHexString(FormatCast.getHexBytes(de.getQuestcode())).equals(FormatCast.bytesToHexString(bytes))) {
                answerString = de.getQuestname();
                if (de.getAnswerlen() == answer.length - 2) {
                    sub += 2;
                    for (DataquestEntity.AnswerinfoEntity ae : de.getAnswerinfo()) {
                        if (ae.getAnswerinforule().equals("hex")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += Integer.toHexString(answer[i] & 0xff).length() < 2 ? ("0" + Integer.toHexString(answer[i] & 0xff)) : Integer.toHexString(answer[i] & 0xff);
                            }
                            sub += ae.getAnswerinfolen();
                            answerString += " ";
                        }
                        if (ae.getAnswerinforule().equals("map")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                switch (answer[sub]) {
                                    case 0:
                                        answerString += ae.getAnswerinfoexplain().getZerozero();
                                        break;
                                    case 1:
                                        answerString += ae.getAnswerinfoexplain().getZeroone();
                                        break;
                                    case 2:
                                        answerString += ae.getAnswerinfoexplain().getZerotwo();
                                        break;
                                    case 3:
                                        answerString += ae.getAnswerinfoexplain().getZerothree();
                                        break;
                                }
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("dec")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            int a = 0;
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                a = a | ((answer[i] & 0xff) << (8 * (sub + ae.getAnswerinfolen() - i - 1)));
                            }
                            answerString += a;
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("ascii")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += (char) answer[i];
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("time")) {
                            answerString += ae.getAnswerinfodesc() + ":" + "20";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += Integer.toString(answer[i] & 0xff).length() < 2 ? ("0" + Integer.toString(answer[i] & 0xff)) : Integer.toString(answer[i] & 0xff);
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("")) {
                            sub += ae.getAnswerinfolen();
                        }
                    }
                    return answerString;
                }
            }
        }
        for (ControlquestEntity ce : commandInfo.getControlquest()) {
            if ((ce.getQuestmedia().equals("both") | ce.getQuestmedia().equals("blue")) && FormatCast.bytesToHexString(FormatCast.getHexBytes(ce.getQuestcode())).equals(FormatCast.bytesToHexString(bytes))) {
                answerString = ce.getQuestname();
                if (ce.getAnswerlen() == answer.length - 2) {
                    sub += 2;
                    for (ControlquestEntity.AnswerinfoEntity ae : ce.getAnswerinfo()) {
                        if (ae.getAnswerinforule().equals("hex")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += Integer.toHexString(answer[i] & 0xff).length() < 2 ? ("0" + Integer.toHexString(answer[i] & 0xff)) : Integer.toHexString(answer[i] & 0xff);
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("map")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                switch (answer[sub]) {
                                    case 0:
                                        answerString += ae.getAnswerinfoexplain().getZerozero();
                                        break;
                                    case 1:
                                        answerString += ae.getAnswerinfoexplain().getZeroone();
                                        break;
                                    case 2:
                                        answerString += ae.getAnswerinfoexplain().getZerotwo();
                                        break;
                                    case 3:
                                        answerString += ae.getAnswerinfoexplain().getZerothree();
                                        break;
                                }
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("dec")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            int a = 0;
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                a = a | ((answer[i] & 0xff) << (8 * (sub + ae.getAnswerinfolen() - i - 1)));
                            }
                            answerString += a;
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("ascii")) {
                            answerString = answerString + ae.getAnswerinfodesc() + ":";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += (char) answer[i];
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("time")) {
                            answerString += ae.getAnswerinfodesc() + ":" + "20";
                            for (int i = sub; i < sub + ae.getAnswerinfolen(); i++) {
                                answerString += Integer.toString(answer[i] & 0xff).length() < 2 ? ("0" + Integer.toString(answer[i] & 0xff)) : Integer.toString(answer[i] & 0xff);
                            }
                            answerString += " ";
                            sub += ae.getAnswerinfolen();
                        }
                        if (ae.getAnswerinforule().equals("")) {
                            sub += ae.getAnswerinfolen();
                        }
                    }
                    return answerString;
                }
            }
        }
        return ANALYSIS_ERROR + ":" + FormatCast.bytesToHexString(answer);
    }

    public void setProtoversion(String protoversion) {
        this.protoversion = protoversion;
    }

    public void setInfoquest(List<InfoquestEntity> infoquest) {
        this.infoquest = infoquest;
    }

    public void setControlquest(List<ControlquestEntity> controlquest) {
        this.controlquest = controlquest;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setErroranswer(List<ErroranswerEntity> erroranswer) {
        this.erroranswer = erroranswer;
    }

    public void setDataquest(List<DataquestEntity> dataquest) {
        this.dataquest = dataquest;
    }

    public String getProtoversion() {
        return protoversion;
    }

    public List<InfoquestEntity> getInfoquest() {
        return infoquest;
    }

    public List<ControlquestEntity> getControlquest() {
        return controlquest;
    }

    public String getVersion() {
        return version;
    }

    public List<ErroranswerEntity> getErroranswer() {
        return erroranswer;
    }

    public List<DataquestEntity> getDataquest() {
        return dataquest;
    }

    public String getDurationMode() {
        return durationMode;
    }

    public void setDurationMode(String durationMode) {
        this.durationMode = durationMode;
    }

    public class InfoquestEntity {
        /**
         * questmedia : both
         * answerinfo : [{"answerinfoexplain":{},"answerinforule":"hex","answerinfodesc":"软件版本号","answerinfolen":1,"answerinfonum":1},{"answerinfoexplain":{"00":"待机","01":"实时采集","02":"同步采集","03":"单机采集"},"answerinforule":"map","answerinfodesc":"设备状态","answerinfolen":1,"answerinfonum":2},{"answerinfoexplain":{},"answerinforule":"dec","answerinfodesc":"电量","answerinfolen":2,"answerinfonum":3}]
         * answerlen : 4
         * answertype : IMMED
         * questname : 查询状态
         * questadditon :
         * questcode : E810
         */
        private String questmedia;
        private List<AnswerinfoEntity> answerinfo;
        private int answerlen;
        private String answertype;
        private String questname;
        private String questadditon;
        private String questcode;

        public void setQuestmedia(String questmedia) {
            this.questmedia = questmedia;
        }

        public void setAnswerinfo(List<AnswerinfoEntity> answerinfo) {
            this.answerinfo = answerinfo;
        }

        public void setAnswerlen(int answerlen) {
            this.answerlen = answerlen;
        }

        public void setAnswertype(String answertype) {
            this.answertype = answertype;
        }

        public void setQuestname(String questname) {
            this.questname = questname;
        }

        public void setQuestadditon(String questadditon) {
            this.questadditon = questadditon;
        }

        public void setQuestcode(String questcode) {
            this.questcode = questcode;
        }

        public String getQuestmedia() {
            return questmedia;
        }

        public List<AnswerinfoEntity> getAnswerinfo() {
            return answerinfo;
        }

        public int getAnswerlen() {
            return answerlen;
        }

        public String getAnswertype() {
            return answertype;
        }

        public String getQuestname() {
            return questname;
        }

        public String getQuestadditon() {
            return questadditon;
        }

        public String getQuestcode() {
            return questcode;
        }

        public class AnswerinfoEntity {
            /**
             * answerinfoexplain : {}
             * answerinforule : hex
             * answerinfodesc : 软件版本号
             * answerinfolen : 1
             * answerinfonum : 1
             */
            private AnswerinfoexplainEntity answerinfoexplain;
            private String answerinforule;
            private String answerinfodesc;
            private int answerinfolen;
            private int answerinfonum;

            public void setAnswerinfoexplain(AnswerinfoexplainEntity answerinfoexplain) {
                this.answerinfoexplain = answerinfoexplain;
            }

            public void setAnswerinforule(String answerinforule) {
                this.answerinforule = answerinforule;
            }

            public void setAnswerinfodesc(String answerinfodesc) {
                this.answerinfodesc = answerinfodesc;
            }

            public void setAnswerinfolen(int answerinfolen) {
                this.answerinfolen = answerinfolen;
            }

            public void setAnswerinfonum(int answerinfonum) {
                this.answerinfonum = answerinfonum;
            }

            public AnswerinfoexplainEntity getAnswerinfoexplain() {
                return answerinfoexplain;
            }

            public String getAnswerinforule() {
                return answerinforule;
            }

            public String getAnswerinfodesc() {
                return answerinfodesc;
            }

            public int getAnswerinfolen() {
                return answerinfolen;
            }

            public int getAnswerinfonum() {
                return answerinfonum;
            }

            public class AnswerinfoexplainEntity {
                @SerializedName("00")
                private String zerozero;
                @SerializedName("01")
                private String zeroone;
                @SerializedName("02")
                private String zerotwo;
                @SerializedName("03")
                private String zerothree;

                public String getZeroone() {
                    return zeroone;
                }

                public String getZerothree() {
                    return zerothree;
                }

                public String getZerotwo() {
                    return zerotwo;
                }

                public String getZerozero() {
                    return zerozero;
                }
            }
        }
    }

    public class ControlquestEntity {
        /**
         * questmedia : usb
         * answerinfo : [{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}]
         * answerlen : 4
         * answertype : IMMED
         * questname : 开始同步采集
         * questadditon :
         * questcode : E821
         */
        private String questmedia;
        private List<AnswerinfoEntity> answerinfo;
        private int answerlen;
        private String answertype;
        private String questname;
        private String questadditon;
        private String questcode;

        public void setQuestmedia(String questmedia) {
            this.questmedia = questmedia;
        }

        public void setAnswerinfo(List<AnswerinfoEntity> answerinfo) {
            this.answerinfo = answerinfo;
        }

        public void setAnswerlen(int answerlen) {
            this.answerlen = answerlen;
        }

        public void setAnswertype(String answertype) {
            this.answertype = answertype;
        }

        public void setQuestname(String questname) {
            this.questname = questname;
        }

        public void setQuestadditon(String questadditon) {
            this.questadditon = questadditon;
        }

        public void setQuestcode(String questcode) {
            this.questcode = questcode;
        }

        public String getQuestmedia() {
            return questmedia;
        }

        public List<AnswerinfoEntity> getAnswerinfo() {
            return answerinfo;
        }

        public int getAnswerlen() {
            return answerlen;
        }

        public String getAnswertype() {
            return answertype;
        }

        public String getQuestname() {
            return questname;
        }

        public String getQuestadditon() {
            return questadditon;
        }

        public String getQuestcode() {
            return questcode;
        }

        public class AnswerinfoEntity {
            /**
             * answerinfoexplain : {}
             * answerinforule :
             * answerinfodesc :
             * answerinfolen : 3
             * answerinfonum : 1
             */
            private AnswerinfoexplainEntity answerinfoexplain;
            private String answerinforule;
            private String answerinfodesc;
            private int answerinfolen;
            private int answerinfonum;

            public void setAnswerinfoexplain(AnswerinfoexplainEntity answerinfoexplain) {
                this.answerinfoexplain = answerinfoexplain;
            }

            public void setAnswerinforule(String answerinforule) {
                this.answerinforule = answerinforule;
            }

            public void setAnswerinfodesc(String answerinfodesc) {
                this.answerinfodesc = answerinfodesc;
            }

            public void setAnswerinfolen(int answerinfolen) {
                this.answerinfolen = answerinfolen;
            }

            public void setAnswerinfonum(int answerinfonum) {
                this.answerinfonum = answerinfonum;
            }

            public AnswerinfoexplainEntity getAnswerinfoexplain() {
                return answerinfoexplain;
            }

            public String getAnswerinforule() {
                return answerinforule;
            }

            public String getAnswerinfodesc() {
                return answerinfodesc;
            }

            public int getAnswerinfolen() {
                return answerinfolen;
            }

            public int getAnswerinfonum() {
                return answerinfonum;
            }

            public class AnswerinfoexplainEntity {
                @SerializedName("00")
                private String zerozero;
                @SerializedName("01")
                private String zeroone;
                @SerializedName("02")
                private String zerotwo;
                @SerializedName("03")
                private String zerothree;

                public String getZeroone() {
                    return zeroone;
                }

                public String getZerothree() {
                    return zerothree;
                }

                public String getZerotwo() {
                    return zerotwo;
                }

                public String getZerozero() {
                    return zerozero;
                }
            }
        }
    }

    public class ErroranswerEntity {
        /**
         * errcode : E8FF
         * errmsg : 指令有误[长度不足、命令字不存在、逻辑错误]
         */
        private String errcode;
        private String errmsg;

        public void setErrcode(String errcode) {
            this.errcode = errcode;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }

        public String getErrcode() {
            return errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }
    }

    public class DataquestEntity {
        /**
         * questmedia : usb
         * answerinfo : [{"answerinfoexplain":{},"answerinforule":"","answerinfodesc":"","answerinfolen":3,"answerinfonum":1},{"answerinfoexplain":{"00":"失败","01":"成功 "},"answerinforule":"map","answerinfodesc":"执行状态","answerinfolen":1,"answerinfonum":2}]
         * answerlen : 4
         * answertype : DELAY
         * questname : 清除存储数据
         * questadditon :
         * questcode : E830
         */
        private String questmedia;
        private List<AnswerinfoEntity> answerinfo;
        private int answerlen;
        private String answertype;
        private String questname;
        private String questadditon;
        private String questcode;

        public void setQuestmedia(String questmedia) {
            this.questmedia = questmedia;
        }

        public void setAnswerinfo(List<AnswerinfoEntity> answerinfo) {
            this.answerinfo = answerinfo;
        }

        public void setAnswerlen(int answerlen) {
            this.answerlen = answerlen;
        }

        public void setAnswertype(String answertype) {
            this.answertype = answertype;
        }

        public void setQuestname(String questname) {
            this.questname = questname;
        }

        public void setQuestadditon(String questadditon) {
            this.questadditon = questadditon;
        }

        public void setQuestcode(String questcode) {
            this.questcode = questcode;
        }

        public String getQuestmedia() {
            return questmedia;
        }

        public List<AnswerinfoEntity> getAnswerinfo() {
            return answerinfo;
        }

        public int getAnswerlen() {
            return answerlen;
        }

        public String getAnswertype() {
            return answertype;
        }

        public String getQuestname() {
            return questname;
        }

        public String getQuestadditon() {
            return questadditon;
        }

        public String getQuestcode() {
            return questcode;
        }

        public class AnswerinfoEntity {
            /**
             * answerinfoexplain : {}
             * answerinforule :
             * answerinfodesc :
             * answerinfolen : 3
             * answerinfonum : 1
             */
            private AnswerinfoexplainEntity answerinfoexplain;
            private String answerinforule;
            private String answerinfodesc;
            private int answerinfolen;
            private int answerinfonum;

            public void setAnswerinfoexplain(AnswerinfoexplainEntity answerinfoexplain) {
                this.answerinfoexplain = answerinfoexplain;
            }

            public void setAnswerinforule(String answerinforule) {
                this.answerinforule = answerinforule;
            }

            public void setAnswerinfodesc(String answerinfodesc) {
                this.answerinfodesc = answerinfodesc;
            }

            public void setAnswerinfolen(int answerinfolen) {
                this.answerinfolen = answerinfolen;
            }

            public void setAnswerinfonum(int answerinfonum) {
                this.answerinfonum = answerinfonum;
            }

            public AnswerinfoexplainEntity getAnswerinfoexplain() {
                return answerinfoexplain;
            }

            public String getAnswerinforule() {
                return answerinforule;
            }

            public String getAnswerinfodesc() {
                return answerinfodesc;
            }

            public int getAnswerinfolen() {
                return answerinfolen;
            }

            public int getAnswerinfonum() {
                return answerinfonum;
            }

            public class AnswerinfoexplainEntity {
                @SerializedName("00")
                private String zerozero;
                @SerializedName("01")
                private String zeroone;
                @SerializedName("02")
                private String zerotwo;
                @SerializedName("03")
                private String zerothree;

                public String getZeroone() {
                    return zeroone;
                }

                public String getZerothree() {
                    return zerothree;
                }

                public String getZerotwo() {
                    return zerotwo;
                }

                public String getZerozero() {
                    return zerozero;
                }
            }
        }
    }
}
